package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机验证码短信
     *
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成四位的验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            //调用阿里云提供的短信服务API完成发送短信
            log.info("code{}", code);
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);
            //保存到session中
            //session.setAttribute(phone, code);
            //保存到redis中
            redisTemplate.opsForValue().set("code_"+phone,code);
            return R.success("发送成功");
        }
        return R.error("发送失败");

    }

    /**
     * 移动端登录请求
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map user, HttpSession session) {
        //拿出手机号
        String phone = user.get("phone").toString();
        //拿出请求验证码
        String LoginCode = user.get("code").toString();
        //1.拿出session中的验证码
        //String code = (String) session.getAttribute(phone);
        //1.拿出redis中的验证码
        String code = (String) redisTemplate.opsForValue().get("code_" + phone);
        //2.校验请求过来的验证码是否正确
        if (code != null && code.equals(LoginCode)) {

            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone, phone);
            User user1 = userService.getOne(queryWrapper);
            if (user1 == null) {
                user1 = new User();
                user1.setPhone(phone);
                user1.setStatus(1);
                userService.save(user1);
            }
            //3.成功之后把user放入session中保存
            session.setAttribute("user", user1.getId());
            //登录完成之后吧redis缓存中的验证码给删掉
            redisTemplate.delete("code_"+phone);
            return R.success(user1);
        }
        return R.error("登录失败");
    }

    //http://localhost:8080/user/loginout
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session) {
        //直接销毁
        //session.invalidate();
        session.removeAttribute("user");
        return R.success("退出成功");
    }


}
